package com.single.weblearn.typehandler;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.TypeHandler;

@MappedJdbcTypes(JdbcType.BIGINT)
public class BigInToDoubleHandler implements TypeHandler<Double> {

	@Override
	public void setParameter(PreparedStatement ps, int i, Double parameter, JdbcType jdbcType) throws SQLException {			    
		ps.setBigDecimal(i, new BigDecimal(Double.toString(parameter*100)));
	}

	@Override
	public Double getResult(ResultSet rs, String columnName) throws SQLException {
		BigDecimal bigDecimal = rs.getBigDecimal(columnName);
		return bigDecimal == null ? null : (bigDecimal.doubleValue())/100;
	}

	@Override
	public Double getResult(ResultSet rs, int columnIndex) throws SQLException {
		 BigDecimal bigDecimal = rs.getBigDecimal(columnIndex);
		 return bigDecimal == null ? null : (bigDecimal.doubleValue())/100;
	}

	@Override
	public Double getResult(CallableStatement cs, int columnIndex) throws SQLException {
		BigDecimal bigDecimal = cs.getBigDecimal(columnIndex);
		return bigDecimal == null ? null : (bigDecimal.doubleValue())/100;
	}

}
